# -*- coding:gb2312 -*-

import os
import time
import threading
import unittest

from FSInfo import FSInfo
from MyLogger import MyLogger, SIZEROTATE, TIMEROTATE

class Monitor(threading.Thread):
    def __init__(self, profile, fsinfo):
        threading.Thread.__init__(self)
        self.fsinfo = fsinfo
        self.logger = MyLogger(os.path.join(profile.log_dir, "SysInfo.log"), SIZEROTATE, 0)
        
    def run(self):
        while(True):
            try:
                self.fsinfo.update_info()
                self.logger.info("Mem_Size: %dM Mem_Used: %d%% Cache_HD_Size: %dG Cache_HD_Used: %d%% Backup_HD_Size: %dG Backup_HD_Used %d%%"
                                 %(self.fsinfo.mem_size, self.fsinfo.mem_used, self.fsinfo.cache_hd_size, 
                                   self.fsinfo.cache_hd_used, self.fsinfo.backup_hd_size, self.fsinfo.backup_hd_used))  
            except:
                self.logger.exception("unknown error")
            time.sleep(5)
        
class MonitorTestCase(unittest.TestCase):
    def setUp(self):
        self.profile = Profile()
        self.fsinfo = FSInfo()
        self.monitor = Monitor(self.profile, self.fsinfo)
        
    def tearDown(self):
        pass
    
    def testBaseFun(self):
        self.monitor.setDaemon(True)
        self.monitor.start()
        time.sleep(10)
        
if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest(MonitorTestCase("testBaseFun"))
    
    runner = unittest.TextTestRunner()
    runner.run(suite)